package bb.lanxing.common.engin.util;

import android.os.Handler;
import android.os.Looper;
import androidx.core.util.Pair;
import com.alibaba.fastjson.JSON;
import gov.nist.core.Separators;
import bb.lanxing.App;
import bb.lanxing.calc.data.SourcePoint;
import bb.lanxing.calc.processer.ProcessorFactory;
import bb.lanxing.calc.processer.i.IWorkoutProcessor;
import bb.lanxing.model.database.IWorkout;
import bb.lanxing.model.database.Workout;
import bb.lanxing.model.jsonlocal.JPoint;
import bb.lanxing.model.jsonlocal.JPointPackage;
import bb.lanxing.util.thread.ThreadPools;
import bb.lanxing.util.Log;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class WorkoutFileSimulator {
    private int cadenceSource;
    private Handler handler;
    private int heartRateSource;
    private int locSource;
    private int pos;
    private IWorkoutProcessor processor;
    private LinkedList<Pair<Integer, SourcePoint>> sourcePoints;
    private long timeDifference = -1;
    private IWorkout workout;
    private List<File> workoutFiles;

    static /* synthetic */ int access$008(WorkoutFileSimulator workoutFileSimulator) {
        int i = workoutFileSimulator.pos;
        workoutFileSimulator.pos = i + 1;
        return i;
    }

    public WorkoutFileSimulator(String str) {
        List<File> asList = Arrays.asList(new File(str).listFiles());
        this.workoutFiles = asList;

        Collections.sort(asList, (file, file2) -> file.getName().compareTo(file2.getName()));
        this.sourcePoints = new LinkedList<>();
        this.handler = new Handler(Looper.getMainLooper());
        this.processor = ProcessorFactory.create(ProcessorFactory.TYPE_RECORD);//2
    }

    public void startSimulator() {
        this.processor.filterPoint(false);
        this.processor.init();
        this.processor.getWorkout();
        this.locSource = 1;
        this.cadenceSource = 2;
        this.heartRateSource = 2;
        this.pos = 0;
        dispatchSourcePoint();
    }

    public synchronized void parse(File file) throws IOException {
        Log.i("engin", "parse file=" + file.toString());
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        StringBuilder sb = new StringBuilder();
        while (true) {
            String readLine = bufferedReader.readLine();
            if (readLine == null) {
                break;
            }
            sb.append(readLine);
        }
        for (JPoint jPoint : ((JPointPackage) JSON.parseObject(sb.toString(), JPointPackage.class)).getPoints()) {
            this.sourcePoints.add(new Pair<>(jPoint.getPointType(), jPoint.toSourcePoint(this.locSource, this.cadenceSource, this.heartRateSource)));
        }
    }

    public synchronized void dispatchSourcePoint() {
        if (this.pos >= this.workoutFiles.size()) {
            if (this.sourcePoints.isEmpty()) {
                this.processor.release();
                Workout workout = this.processor.getWorkout();
                App context = App.getContext();
                context.showLongMessage("save finished! " + workout.getId() + Separators.COLON + workout.getTitle());
                this.handler.removeCallbacksAndMessages(null);
                return;
            }
        } else if (this.sourcePoints.size() < 10) {
            ThreadPools.getInstance().addTask(() -> {
                try {
                    WorkoutFileSimulator workoutFileSimulator = WorkoutFileSimulator.this;
                    workoutFileSimulator.parse((File) workoutFileSimulator.workoutFiles.get(WorkoutFileSimulator.access$008(WorkoutFileSimulator.this)));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
        }
        if (!this.sourcePoints.isEmpty()) {
            Pair<Integer, SourcePoint> remove = this.sourcePoints.remove();
            if (remove.first != 0) {
                Log.i("engin", "workout pause & resume");
                SourcePoint sourcePoint = remove.second;
                adjustTime(sourcePoint);
                this.processor.process(sourcePoint);
                this.processor.flushPoint(true);
                if (remove.first == 1) {
                    this.processor.release();
                }
                Workout workout2 = this.processor.getWorkout();
                IWorkoutProcessor create = ProcessorFactory.create(ProcessorFactory.TYPE_RECORD);//2
//                processor = create;
                if (processor != null) {
                    processor.filterPoint(false);
                    processor.init(null, workout2);
                }
            } else {
                SourcePoint sourcePoint2 = remove.second;
                adjustTime(sourcePoint2);
                this.processor.process(sourcePoint2);
            }
        }

        this.handler.postDelayed(() -> WorkoutFileSimulator.this.dispatchSourcePoint(), 5L);
    }

    private void adjustTime(SourcePoint sourcePoint) {
        if (this.timeDifference < 0) {
            long currentTimeMillis = System.currentTimeMillis();
            this.timeDifference = currentTimeMillis - sourcePoint.getTimestamp();
            sourcePoint.setTimestamp(currentTimeMillis);
            return;
        }
        sourcePoint.setTimestamp(sourcePoint.getTimestamp() + this.timeDifference);
    }
}
